
package easim.examples;

import easim.net.Node;
import jist.runtime.JistAPI.DoNotRewrite;
import easim.win.SynControler;
import easim.win.EditControler;

import jist.runtime.JistAPI.DoNotRewrite;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PopupMenu3 extends EditControler.PopupMenu 
    implements ActionListener, jist.runtime.JistAPI.DoNotRewrite
{

  protected Node node;

  public void setNode(Node node) {
    this.node = node;
  }

  protected static class Interactor0 implements SynControler.Interactor, DoNotRewrite
  {
    public Node[] nodes;
    public void interactorAction() {
      for (int i = 0; i < nodes.length; ++ i) {
        Node3 node = (Node3)nodes[i];
        node.start();
      }
    }
  }

  protected static class Interactor1 implements SynControler.Interactor, DoNotRewrite
  {
    public Node[] nodes;
    public void interactorAction() {
      for (int i = 0; i < nodes.length; ++ i) {
        Node3 node = (Node3)nodes[i];
        node.mobility.start();
      }
    }
  }

  protected static class Interactor2 implements SynControler.Interactor, DoNotRewrite
  {
    public Node[] nodes;
    public void interactorAction() {
      for (int i = 0; i < nodes.length; ++ i) {
        Node3 node = (Node3)nodes[i];
        node.mobility.stop();
      }
    }
  }

  public PopupMenu3() {
    JMenuItem start = new JMenuItem("Start All");
    start.addActionListener(this);
    add(start);
    JMenuItem startMobility = new JMenuItem("Start All Mobility");
    startMobility.addActionListener(this);
    add(startMobility);
    JMenuItem stopMobility = new JMenuItem("Stop All Mobility");
    stopMobility.addActionListener(this);
    add(stopMobility);
  }

  public void actionPerformed(ActionEvent ev) {
    String cmd = ev.getActionCommand();
    if (cmd.equals("Start All")) {
      Interactor0 interactor = new Interactor0();
      interactor.nodes = SynControler.getAllNodes();
      SynControler.interact(interactor);
    }
    else if (cmd.equals("Start All Mobility")) {
      Interactor1 interactor = new Interactor1();
      interactor.nodes = SynControler.getAllNodes();
      SynControler.interact(interactor);
    }
    else if (cmd.equals("Stop All Mobility")) {
      Interactor2 interactor = new Interactor2();
      interactor.nodes = SynControler.getAllNodes();
      SynControler.interact(interactor);
    }
  }

}
